home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / game / shoot / ADescentSrc.lha / descent / bios / mouse.c < prev    next >
C/C++ Source or Header  |  1998-08-10  |  11KB  |  476 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: /usr/CVS/descent/bios/mouse.c,v $
  15.  * $Revision: 1.4 $
  16.  * $Author: tfrieden $
  17.  * $Date: 1998/08/09 23:02:11 $
  18.  * 
  19.  * Functions to access Mouse and Cyberman...
  20.  * 
  21.  * $Log: mouse.c,v $
  22.  * Revision 1.4  1998/08/09 23:02:11  tfrieden
  23.  * Some functions used by the editor
  24.  *
  25.  * Revision 1.3  1998/03/14 13:42:40  hfrieden
  26.  * Fixed merge conflicts
  27.  *
  28.  * Revision 1.2  1998/03/13 23:48:49  tfrieden
  29.  * fixed hat problems
  30.  *
  31.  * Revision 1.1.1.1  1998/03/03 15:11:49  nobody
  32.  * reimport after crash from backup
  33.  *
  34.  */
  35.  
  36.  
  37. #pragma off (unreferenced)
  38. static char rcsid[] = "$Id: mouse.c,v 1.4 1998/08/09 23:02:11 tfrieden Exp $";
  39. #pragma on (unreferenced)
  40.  
  41. #include <clib/timer_protos.h>
  42. #include <inline/intuition.h>
  43. #include <intuition/intuition.h>
  44. #include <inline/exec.h>
  45. #include <exec/exec.h>
  46. #include <exec/interrupts.h>
  47. #include <devices/input.h>
  48. #include <devices/inputevent.h>
  49. #include <clib/alib_protos.h>
  50.  
  51. #include <stdlib.h>
  52. #include <stdio.h>
  53. #include <string.h>
  54.  
  55. #include "error.h"
  56. #include "fix.h"
  57. #include "mouse.h"
  58. #include "timer.h"
  59.  
  60. #define ME_CURSOR_MOVED (1<<0)
  61. #define ME_LB_P             (1<<1)
  62. #define ME_LB_R             (1<<2)
  63. #define ME_RB_P             (1<<3)
  64. #define ME_RB_R             (1<<4)
  65. #define ME_MB_P             (1<<5)
  66. #define ME_MB_R             (1<<6)
  67. #define ME_OB_P             (1<<7)
  68. #define ME_OB_R             (1<<8)
  69. #define ME_X_C          (1<<9)
  70. #define ME_Y_C          (1<<10)
  71. #define ME_Z_C          (1<<11)
  72. #define ME_P_C          (1<<12)
  73. #define ME_B_C          (1<<13)
  74. #define ME_H_C          (1<<14)
  75. #define ME_O_C          (1<<15)
  76.  
  77. #define MOUSE_MAX_BUTTONS   11
  78.  
  79. extern int gr_mouse_reported;
  80.  
  81. typedef struct event_info {
  82.     short x;
  83.     short y;
  84.     short z;
  85.     short pitch;
  86.     short bank;
  87.     short heading;
  88.     ushort button_status;
  89.     ushort device_dependant;
  90. } event_info;
  91.  
  92. typedef struct mouse_info {
  93.     fix     ctime;
  94.     ubyte       cyberman;
  95.     int     num_buttons;
  96.     ubyte       pressed[MOUSE_MAX_BUTTONS];
  97.     fix     time_went_down[MOUSE_MAX_BUTTONS];
  98.     fix     time_held_down[MOUSE_MAX_BUTTONS];
  99.     uint        num_downs[MOUSE_MAX_BUTTONS];
  100.     uint        num_ups[MOUSE_MAX_BUTTONS];
  101.     event_info *x_info;
  102.     ushort  button_status;
  103.     int x,y;
  104. } mouse_info;
  105.  
  106. typedef struct cyberman_info {
  107.     ubyte device_type;
  108.     ubyte major_version;
  109.     ubyte minor_version;
  110.     ubyte x_descriptor;
  111.     ubyte y_descriptor;
  112.     ubyte z_descriptor;
  113.     ubyte pitch_descriptor;
  114.     ubyte roll_descriptor;
  115.     ubyte yaw_descriptor;
  116.     ubyte reserved;
  117. } cyberman_info;
  118.  
  119. static mouse_info Mouse;
  120.  
  121. extern struct Library *IntuitionBase;
  122. extern struct Library *SysBase;
  123. extern struct Library *TimerBase;
  124. #ifndef VIRGIN
  125. extern struct Screen *scr;
  126. #endif
  127. struct Interrupt *inputHandler;
  128. struct IOStdReq *inputReqBlk;
  129. struct MsgPort *inputPort;
  130. BYTE port = 0;
  131. struct IEPointerPixel NewPixel;
  132. struct InputEvent FakeEvent;
  133.  
  134. static int Mouse_installed = 0;
  135. char NameString[] = "Descent input handler";
  136.  
  137. extern int NumAxis;
  138.  
  139.  
  140. void mouse_handler_end (void)  // dummy functions
  141. {
  142. }
  143.  
  144. struct InputEvent *  __saveds __interrupt
  145. mouseHandlerCode(struct InputEvent *oldEventChain __asm("a0"), void *data __asm("a1"))
  146. {
  147. #ifndef VIRGIN
  148.     mouse_info *mouse = (mouse_info *)data;
  149.     struct InputEvent *akt;
  150.     struct timeval *tv;
  151.     fix time;
  152.     int *x = 0;
  153.  
  154.     tv = &(oldEventChain->ie_TimeStamp);
  155.     time = (fix)(tv->tv_secs&0xffff)<<16;
  156.     time = (fix)(((float)tv->tv_micro/1000000.0)*65536.0)+time;
  157.  
  158.     mouse->ctime = time;
  159.  
  160.     for (akt = oldEventChain; akt; akt = akt->ie_NextEvent) {
  161.         if (akt->ie_Class == IECLASS_RAWMOUSE) {
  162. /*            if (akt->ie_Qualifier == IEQUALIFIER_RELATIVEMOUSE) {
  163.                 mouse->x += akt->ie_X;
  164.                 mouse->y += akt->ie_Y;
  165.             } else {
  166.                 mouse->x = akt->ie_X;
  167.                 mouse->y = akt->ie_Y;
  168.             }*/
  169.             switch(akt->ie_Code) {
  170.                 case IECODE_LBUTTON:
  171.                     mouse->pressed[MB_LEFT] = 1;
  172.                     mouse->time_went_down[MB_LEFT] = mouse->ctime;
  173.                     mouse->num_downs[MB_LEFT] ++;
  174.                     break;
  175.                 case IECODE_LBUTTON|IECODE_UP_PREFIX:
  176.                     mouse->pressed[MB_LEFT] = 0;
  177.                     mouse->time_held_down[MB_LEFT] += mouse->ctime - mouse->time_went_down[MB_LEFT];
  178.                     mouse->num_ups[MB_LEFT] ++;
  179.                     break;
  180.                 case IECODE_RBUTTON:
  181.                     mouse->pressed[MB_RIGHT] = 1;
  182.                     mouse->time_went_down[MB_RIGHT] = mouse->ctime;
  183.                     mouse->num_downs[MB_RIGHT] ++;
  184.                     break;
  185.                 case IECODE_RBUTTON|IECODE_UP_PREFIX:
  186.                     mouse->pressed[MB_RIGHT] = 0;
  187.                     mouse->time_held_down[MB_RIGHT] += mouse->ctime - mouse->time_went_down[MB_RIGHT];
  188.                     mouse->num_ups[MB_RIGHT] ++;
  189.                     break;
  190.                 case IECODE_MBUTTON:
  191.                     mouse->pressed[MB_MIDDLE] = 1;
  192.                     mouse->time_went_down[MB_MIDDLE] = mouse->ctime;
  193.                     mouse->num_downs[MB_MIDDLE] ++;
  194.                     break;
  195.                 case IECODE_MBUTTON|IECODE_UP_PREFIX:
  196.                     mouse->pressed[MB_MIDDLE] = 0;
  197.                     mouse->time_held_down[MB_MIDDLE] += mouse->ctime - mouse->time_went_down[MB_MIDDLE];
  198.                     mouse->num_ups[MB_MIDDLE] ++;
  199.                     break;
  200.             }
  201.         }
  202.     }
  203.  
  204.     mouse->button_status = 0;
  205.     if (mouse->pressed[MB_LEFT] != 0)    mouse->button_status |= MOUSE_LBTN;
  206.     if (mouse->pressed[MB_RIGHT] != 0)   mouse->button_status |= MOUSE_RBTN;
  207.     if (mouse->pressed[MB_MIDDLE] != 0)  mouse->button_status |= MOUSE_MBTN;
  208.  
  209.     return(oldEventChain);
  210. #endif
  211. }
  212.  
  213.  
  214. //--------------------------------------------------------
  215. // returns 0 if no mouse
  216. //           else number of buttons
  217. int mouse_init(int enable_cyberman)
  218. {
  219.     atexit( mouse_close );
  220.  
  221.     memset(&Mouse, 0, sizeof(mouse_info));
  222. #ifndef VIRGIN
  223.  
  224. #ifdef DEBUG_MOUSE
  225.     fprintf( stderr, "Initialising Mouse handler\n");
  226. #endif
  227.  
  228.     inputPort = CreatePort(NULL, NULL);
  229.     if (!inputPort) return 0;
  230.  
  231. #ifdef DEBUG_MOUSE
  232.     fprintf( stderr, "Got port\n");
  233. #endif
  234.  
  235.     inputHandler = malloc(sizeof(struct Interrupt));
  236.     if (!inputHandler) {
  237.         DeletePort(inputPort);
  238.         return 0;
  239.     }
  240. #ifdef DEBUG_MOUSE
  241.     fprintf( stderr, "Got Interrupt structure\n");
  242. #endif
  243.  
  244.     inputReqBlk = (struct IOStdReq *)CreateExtIO(inputPort, sizeof(struct IOStdReq));
  245.     if (!inputReqBlk) {
  246.         DeletePort(inputPort);
  247.         free(inputHandler);
  248.         return 0;
  249.     }
  250.  
  251. #ifdef DEBUG_MOUSE
  252.     fprintf( stderr, "Got ExtIo\n");
  253. #endif
  254.  
  255.     if (OpenDevice("input.device", NULL, (struct IORequest *)inputReqBlk, NULL)) {
  256.         DeleteExtIO((struct IORequest *)inputReqBlk);
  257.         DeletePort(inputPort);
  258.         free(inputHandler);
  259.         return 0;
  260.     }
  261.  
  262. #ifdef DEBUG_MOUSE
  263.     fprintf( stderr, "Installing Mouse handler\n");
  264. #endif
  265.  
  266. /*    Mouse.x = scr->MouseX;
  267.     Mouse.y = scr->MouseY; */
  268.  
  269.     inputHandler->is_Code = mouseHandlerCode;
  270.     inputHandler->is_Data = (void *)&Mouse;
  271.     inputHandler->is_Node.ln_Pri = 100;
  272.     inputHandler->is_Node.ln_Name = (UBYTE *)NameString;
  273.     inputReqBlk->io_Data = (APTR)inputHandler;
  274.     inputReqBlk->io_Command = IND_ADDHANDLER;
  275.     DoIO((struct IORequest *)inputReqBlk);
  276.  
  277.     Mouse_installed = 1;
  278.  
  279. #ifdef DEBUG_MOUSE
  280.     fprintf(stderr, "Setting up fake events\n");
  281. #endif
  282.  
  283.     FakeEvent.ie_EventAddress = (APTR)&NewPixel;
  284.     FakeEvent.ie_NextEvent = NULL;
  285.     FakeEvent.ie_Class = IECLASS_NEWPOINTERPOS;
  286.     FakeEvent.ie_SubClass = IESUBCLASS_PIXEL;
  287.     FakeEvent.ie_Code = IECODE_NOBUTTON;
  288.     FakeEvent.ie_Qualifier = NULL;
  289.  
  290.  
  291. #ifdef DEBUG_MOUSE
  292.     fprintf( stderr, "Done\n");
  293. #endif
  294. #endif
  295.     return 3;
  296.  
  297. }
  298.  
  299.  
  300. void mouse_switch_on()
  301. {
  302.  
  303.     if (Mouse_installed == 0 || NumAxis <= 2) return;
  304.  
  305.     port = 0;
  306.     inputReqBlk->io_Data = &port;
  307.     inputReqBlk->io_Flags = IOF_QUICK;
  308.     inputReqBlk->io_Length = 1;
  309.     inputReqBlk->io_Command = IND_SETMPORT;
  310.     BeginIO((struct IORequest *)inputReqBlk);
  311.     if (inputReqBlk->io_Error == 0)
  312.         printf("Mouse on\n");
  313. }
  314.  
  315.  
  316. void mouse_switch_off()
  317. {
  318.  
  319.     if (Mouse_installed == 0 || NumAxis <= 2) return;
  320.  
  321.     port = -1;
  322.     inputReqBlk->io_Data = &port;
  323.     inputReqBlk->io_Flags = IOF_QUICK;
  324.     inputReqBlk->io_Length = 1;
  325.     inputReqBlk->io_Command = IND_SETMPORT;
  326.     BeginIO((struct IORequest *)inputReqBlk);
  327.     if (inputReqBlk->io_Error == 0)
  328.         printf("Mouse off\n");
  329. }
  330.  
  331.  
  332. void mouse_close()
  333. {
  334.  
  335.     mouse_switch_on();
  336.  
  337.     Mouse_installed = 0;
  338. #ifndef VIRGIN
  339.     inputReqBlk->io_Data = (APTR) inputHandler;
  340.     inputReqBlk->io_Command = IND_REMHANDLER;
  341.     DoIO((struct IORequest *)inputReqBlk);
  342.  
  343.     CloseDevice((struct IORequest *)inputReqBlk);
  344.     DeleteExtIO((struct IORequest *)inputReqBlk);
  345.     DeletePort(inputPort);
  346.     free(inputHandler);
  347. #endif
  348. }
  349.  
  350. int mouse_set_limits( int x1, int y1, int x2, int y2 )
  351. {
  352.     return 0;
  353. }
  354.  
  355. void mouse_center()
  356. {
  357. #ifndef VIRGIN
  358.  
  359.     if (Mouse_installed == 0) return;
  360.     if (gr_mouse_reported == 1) return;
  361.  
  362.     NewPixel.iepp_Screen = scr;
  363.     NewPixel.iepp_Position.X = scr->Width / 2;
  364.     NewPixel.iepp_Position.Y = scr->Height / 2;
  365.  
  366.     inputReqBlk->io_Data = (APTR)&FakeEvent;
  367.     inputReqBlk->io_Length = sizeof(struct InputEvent);
  368.     inputReqBlk->io_Command = IND_WRITEEVENT;
  369.     DoIO((struct IORequest *)inputReqBlk);
  370. #endif
  371. }
  372.  
  373. void mouse_get_pos( int *x, int *y)
  374. {
  375. #ifndef VIRGIN
  376.     if (Mouse_installed) {
  377.         *x = scr->MouseX;
  378.         *y = scr->MouseY;
  379.     } else {
  380.         *x = 0;
  381.         *y = 0;
  382.     }
  383. #else
  384.     *x=0; *y=0;
  385. #endif
  386. }
  387.  
  388. void mouse_get_delta( int *dx, int *dy )
  389. {
  390. #ifndef VIRGIN
  391.     if (Mouse_installed) {
  392.         *dx = -(Mouse.x  - scr->MouseX);
  393.         *dy = -(Mouse.y  - scr->MouseY);
  394.         mouse_center();
  395.         Mouse.x = scr->MouseX;
  396.         Mouse.y = scr->MouseY;
  397.     } else {
  398.         *dx = 0;
  399.         *dy = 0;
  400.     }
  401. #else
  402.     *dx=0;
  403.     *dy=0;
  404. #endif
  405. }
  406.  
  407. int mouse_get_btns()
  408. {
  409.     int status = 0;
  410.  
  411.     return Mouse.button_status;
  412. }
  413.  
  414. void mouse_set_pos( int x, int y)
  415. {
  416. }
  417.  
  418.  
  419.  
  420.  
  421. void mouse_flush()
  422. {
  423.     int i;
  424.     fix CurTime;
  425.  
  426.     if (Mouse_installed == 0) return;
  427.  
  428.     memset(&Mouse, 0, sizeof(mouse_info));
  429.     return;
  430.  
  431.     if (!Mouse_installed)
  432.         return;
  433.  
  434.     CurTime = timer_get_fixed_seconds();
  435.  
  436.     for (i = 0; i < MOUSE_MAX_BUTTONS; i++) {
  437.         Mouse.pressed[i] = 0;
  438.         Mouse.time_went_down[i] = 0; //CurTime;
  439.         Mouse.time_held_down[i] = 0;
  440.         Mouse.num_downs[i] = 0;
  441.         Mouse.num_ups[i] = 0;
  442.     }
  443.     Mouse.button_status = 0;
  444. }
  445.  
  446.  
  447. // Returns how many times this button has went down since last call.
  448. int mouse_button_down_count(int button) 
  449. {
  450.     int count;
  451.  
  452.     count = Mouse.num_downs[button];
  453.     Mouse.num_downs[button] = 0;
  454.  
  455.     return count;
  456. }
  457.  
  458. // Returns 1 if this button is currently down
  459. int mouse_button_state(int button)  
  460. {
  461.     return Mouse.pressed[button];
  462. }
  463.  
  464.  
  465.  
  466. // Returns how long this button has been down since last call.
  467. fix mouse_button_down_time(int button)  
  468. {
  469.     return Mouse.time_held_down[button];
  470. }
  471.  
  472. void mouse_get_cyberman_pos( int *x, int *y )
  473. {
  474. }
  475.  
  476.